#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "chat.h"
#include "login.h"
#include "user.h"
#include "single.h"
#include "historical.h"

Chat *chat = nullptr;
single* singlewindow=nullptr;
mainwindow::mainwindow(QTcpSocket* sk, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mainwindow)
{
    socket =sk;
    ui->setupUi(this);
    QPixmap image(":/headshot/1.png");
    ui->imageLabel->setPixmap(image);
    ui->imageLabel->setScaledContents(true);
    extern User mainuser;//引入全局变量主用户
    QString username = mainuser.getName();
    QString usersignature =mainuser.getSignature();
    ui->namelabel->setText(username);   //设置用户名
    ui->signaturelineEdit->setText(usersignature);  //设置签名
    if(usersignature=="")
        ui->signaturelineEdit->setText("请填写签名");
    QListWidgetItem *item1 = new QListWidgetItem(" 10.194.56.242");
    item1->setSizeHint(QSize(250, 60));//设置宽度为250像素，高度为30像素
    item1->setFont(QFont("Unbuntu",18,-1,false));//设置好友栏字体大小
    QPixmap pixmap1(":/headshot/0.png");// 创建图标对象
    item1->setIcon(QIcon(pixmap1));// 设置图标
    QListWidgetItem *item2 = new QListWidgetItem("10.194.187.6");
    item2->setSizeHint(QSize(250, 60));//设置宽度为250像素，高度为30像素
    item2->setFont(QFont("Unbuntu",18,-1,false));//设置好友栏字体大小
    QPixmap pixmap2(":/headshot/0.png");// 创建图标对象
    item2->setIcon(QIcon(pixmap2));// 设置图标
    QListWidgetItem *item3 = new QListWidgetItem(" 10.194.173.159");
    item3->setSizeHint(QSize(250, 60));//设置宽度为250像素，高度为30像素
    item3->setFont(QFont("Unbuntu",18,-1,false));//设置好友栏字体大小
    QPixmap pixmap3(":/headshot/0.png");// 创建图标对象
    item3->setIcon(QIcon(pixmap3));// 设置图标
    ui->friendlistWidget->addItem(item1);
    ui->friendlistWidget->addItem(item2);
    ui->friendlistWidget->addItem(item3);
    connect(ui->friendlistWidget, &QListWidget::itemDoubleClicked, this, &mainwindow::onItemDoubleClicked);
}
mainwindow::~mainwindow()
{
    delete ui;
}

void mainwindow::on_pushButton_clicked()//更换头像
{
    i++;
    QString path  = QString(":/headshot/%1.png").arg(i);
    if(i==15)
        i=1;
    QPixmap image(path);
    ui->imageLabel->setPixmap(image);
}

void mainwindow::on_chatroompushButton_clicked()//进入聊天室
{
    if (!chat) {//如果有创建过聊天室就不再创建
        chat = new Chat(socket);
    }
    chat->show();
}

void mainwindow::on_signaturelineEdit_editingFinished()//更改签名
{
    QString usersignature = ui->signaturelineEdit->text();
    QSqlQuery query(db);
    QString sqlupdate = QString("update regester set signature ='%1' where id = %2; ").arg(usersignature).arg(mainuser.getId());// 更新签名
    if(!query.exec(sqlupdate))
    {
        qDebug()<<"更改失败";
    }
    else
        qDebug()<<"更改成功";
}
void mainwindow::on_pushButton_2_clicked()
{
    Historical* his_window = new Historical();
    his_window->show();
}
//在列表展示当前连接的其他用户的ip
void mainwindow::showIP(QByteArray b)
{
    //char cmd=b[3];
    QByteArray arrdata=b.mid(4);
    qDebug()<<arrdata;
    QListWidgetItem *item1 = new QListWidgetItem(arrdata);
    item1->setSizeHint(QSize(250, 60));//设置宽度为250像素，高度为30像素
    item1->setFont(QFont("Unbuntu",18,-1,false));//设置好友栏字体大小
    QPixmap pixmap1(":/headshot/0.png");// 创建图标对象
    item1->setIcon(QIcon(pixmap1));// 设置图标
    ui->friendlistWidget->addItem(item1);
}
//双击好友列表栏进入单聊界面
void mainwindow::onItemDoubleClicked(QListWidgetItem *item)
    {
        qDebug() << "Item double-clicked: " << item->text();
    if(!singlewindow)
        {
          singlewindow = new single(socket,item->text());
        }
        singlewindow->setItemText(item->text());
        QByteArray friendIP;
        friendIP.append(item->text());
        quint16 l=friendIP.size()+4;
        QByteArray sendbuffer;
        sendbuffer.append(0x64);//代表ip信息
        sendbuffer.append(static_cast<char>(l&0x00FF));
        sendbuffer.append(static_cast<char>(l>>8&0x00FF));
        sendbuffer.append(0x03);
        sendbuffer.append(friendIP);
        socket->write(sendbuffer);
        singlewindow->show();
    }

